package com.example.testproj.windowsManager;

import android.graphics.PixelFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;

import com.example.testproj.R;

public class WindowsManagerTestActivity extends AppCompatActivity {

    private Button mFloatingButton;
    private WindowManager.LayoutParams mLayoutParams;
    private WindowManager mWindowManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_windows_manager_test);
        mWindowManager = getWindowManager();
        mFloatingButton = new Button(this);
        mFloatingButton.setText("button");
        mLayoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,0,0, PixelFormat.TRANSPARENT);
        mLayoutParams.flags= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mLayoutParams.gravity= Gravity.LEFT|Gravity.TOP;
        mLayoutParams.x=100;
        mLayoutParams.y=300;
        mLayoutParams.type=99;
        mWindowManager.addView(mFloatingButton,mLayoutParams);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int width=mFloatingButton.getMeasuredWidth();
        int height=mFloatingButton.getMeasuredHeight();
        mLayoutParams.x= (int) event.getX()-width/2;
        mLayoutParams.y= (int) event.getY()-height/2;
        mWindowManager.updateViewLayout(mFloatingButton,mLayoutParams);
        return true;
    }
}
